from utils.response import APIResponse
from utils.views import CommonListModelMixin
from .models import Banner
from .serializer import BannerSerializer
from rest_framework.viewsets import GenericViewSet
from django.conf import settings

from django.core.cache import cache


class BannerView(GenericViewSet, CommonListModelMixin):
    queryset = Banner.objects.all().filter(is_delete=False, is_show=True).order_by('orders')[:settings.BANNER_COUNT]
    serializer_class = BannerSerializer

    def list(self, request, *args, **kwargs):
        result = cache.get('banner_list')
        if result:
            print('走了缓存')
            return APIResponse(result=result)
        else:
            print('走了数据库')
            res = super().list(request, *args, **kwargs)
            result = res.data.get('result')
            cache.set('banner_list', result)
            return res
